let pagingData = accountData

function showTable(data) {
    $('.table_content').html('')
    showID() //重新渲染每个的ID值
    for (i = 0; i < data.length; i++) {
        if (data[i].state == '禁用') {
            data[i].state = `<span class="iconfont icon-anniu_kaiqi" style="display:none;"></span><span class="iconfont icon-anniu_guanbi" style="display:block"></span>`
        } else {
            data[i].state = `<span class="iconfont icon-anniu_kaiqi"></span><span class="iconfont icon-anniu_guanbi"></span>`
        }

        $('.table_content').append(`
        <tr>
            <td>
                <input type="checkbox" value="${data[i].id}" name="check_table">
            </td>
            <td>${data[i].id}</td>
            <td>${data[i].org}</td>
            <td>${data[i].name}</td>
            <td>${data[i].phNum}</td>
            <td>${data[i].time}</td>
            <td>${data[i].state}</td>
            <td>
            <span class="shanchu_table" type="button" data-toggle="modal" data-target="#shanchu_table${data[i].id}">删除
            </span><span class="bianji_table"><a href="./bianJiZhangHu.html">编辑</a></span></td>
            <td><span><a href="./zhangHaoXiangQing.html">查看详情</a></span></td>
        </tr>
        `)
    }
}

let num = 1 //初始页
let lengthPage //最大页数
    // 渲染页数
let disPage = 10
$('.page_data').click(function() {
    disPage = parseInt($(this).attr('data-num'))
    paging(num, pagingData, disPage)
})

function showPage(data, disPage) {
    $('.pagination').html(`    <li class="prePage">
                            <a href="#" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                      
                        <li class="nextPage">
                            <a href="#" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>`) //每次渲染前刷新一下渲染的内容

    lengthPage = Math.ceil(data.length / disPage) //lengthPage是最大页数

    for (let i = 1; i <= lengthPage; i++) {
        let pageCont = $('.pagination li:last-of-type').before(`
            <li class="new_page"><a href="#">${i}</a></li>
            `)
        if (lengthPage <= 5) {
            pageCont
        } else if (lengthPage > 5) {
            if (num <= 2) {
                pageCont
                $('.new_page').eq(num + 2).children().text(`...`)
                $('.new_page').slice(num + 3, lengthPage - 1).css({
                    display: 'none'
                })
            } else if (num > 2 && num < 4) {
                $('.new_page').eq(num + 1).children().text(`...`)
                $('.new_page').slice(num + 2, lengthPage - 1).css({
                    display: 'none'
                })
            } else if (num >= 4) {
                pageCont
                if (num < lengthPage - 2) {
                    $('.new_page').eq(1).children().text(`...`)
                    $('.new_page').eq(num + 1).children().text(`...`)
                    $('.new_page').slice(num + 2, lengthPage - 1).css({
                        display: 'none'
                    })
                    $('.new_page').slice(2, num - 2).css({
                        display: 'none'
                    })
                } else if (num <= lengthPage && num > lengthPage - 2) {
                    $('.new_page').eq(1).children().text(`...`)
                    $('.new_page').slice(2, lengthPage - 3).css({
                        display: 'none'
                    })
                } else {
                    $('.new_page').eq(1).children().text(`...`)
                    $('.new_page').slice(2, num - 2).css({
                        display: 'none'
                    })
                }
            }
        }
    }
    $('.new_page').eq(num - 1).toggleClass('active')
}
showPage(pagingData, disPage)
let start //每一页的起始坐标
let checkedBoole


function paging(id, data, disPage) { // 修改每一页显示的内容
    start = (id - 1) * disPage //每一页的起始下标为页面值减1再乘5，
    let arr = data.slice(start, start + disPage)
    showTable(arr)

    $('.quanxuan').click(function() { //全选按钮
        if ($(this).prop('checked') == true) {
            $(".table_content input").prop("checked", true)
        } else {
            $(".table_content input").prop("checked", false)
        }
    })
    showPage(pagingData, disPage)
}

$('.shanchu').click(function() {
    if (confirm('是否确定删除？')) {
        $("input[name='check_table']:checked").each(function() { // 遍历选中的checkbox
            let n = $(this).parents("tr").index(); // 获取checkbox所在行的顺序
            let m = parseInt($(".table_content").find("tr:eq(" + n + ")").children().eq(1).text()) //所在行的id值
            $(".table_content").find("tr:eq(" + n + ")").remove();
            for (let i = 0; i < accountData.length; i++) {
                if (m == pagingData[i].id) {
                    pagingData.splice(i, 1)
                    i--
                    break
                }
            }
        });
        paging(num, pagingData, disPage)
        alert('删除成功')
    }
});


paging(1, pagingData, disPage)
    // 点击页数跳转到对应的页数
$('.pagination').on('click', '.new_page', function() {
        if ($(this).children().text() != '...') {
            $(this).toggleClass('active')
            num = parseInt($(this).text())
            paging($(this).text(), pagingData, disPage)
        }
    })
    // 点击上一页跳转的上一页
$('.pagination').on('click', '.prePage', function() {
        num > 1 ? num-- && paging(num, pagingData, disPage) : paging(1, pagingData, disPage)
    })
    // 点击下一页跳转的下一页
$('.pagination').on('click', '.nextPage', function() {
    num < lengthPage ? num++ && paging(num, pagingData, disPage) : paging(lengthPage, pagingData, disPage)
})
let setRole
$('.set_role').click(function() {
    setRole = $(this).parent().parent()
})

let opVal
$('.org_option').click(function() {
    opVal = $(this).val()
})


$('.password_sure').click(function() {
    $('#myModal').modal('hide') //手动隐藏模态框
})

$('.table_content').on('click', '.shanchu_table', function() { //删除
    if (confirm('是否确定删除？')) {
        let idNum = parseInt($(this).parent().siblings().eq(1).text())
        $(this).parent().parent().remove()
        pagingData.splice(idNum - 1, 1)
        paging(num, pagingData)
        showPage(pagingData)
        alert('删除成功')
    }
})


$('.table_content').on('click', '.bianji_table', function() { //编辑
    paging(num, pagingData)
    showPage(pagingData)
})